<template>
	<!-- 最外层设置一个登录面板的div:login-container -->
	<div class="login-container">
		<!-- 登录盒子 -->
		<div class="login_box">

			<!-- 头像区  -->
			<div class="avatar_box">
				<img src="../assets/111.png" alt />
				<!-- 添加一个图片源 -->
			</div>
			<!--表单区域-->
			<el-form ref="loginFormRef" :model="loginform" :rules="loginRules" class="login_from" label-width="0">
				<!-- 用户名 -->
				<el-form-item prop="userId">
					<el-input v-model="loginform.userId" prefix-icon="iconfont icon-yonghu"></el-input>
					<!-- <span id="userInfo"></span> -->
				</el-form-item>
				<!-- 密码 -->
				<el-form-item prop="password">
					<el-input type="password" v-model="loginform.password" prefix-icon="el-icon-key"></el-input>
					<!-- <span id="pwdInfo"></span> -->
				</el-form-item>
				<!-- 按钮 -->
				<el-form-item class="btns">
					<el-button type="primary" @click="login">登录</el-button>
					<el-button type="info" @click="resetLoginForm">重置</el-button>
					<el-button type="primary" @click="Register" >注册</el-button> 
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				//表单数据
				loginform: {
					userId: 'admin',
					password: '123456',
				},
				//验证对象
				loginRules: {
					//校验用户名
					userId: [{
							required: true,
							message: '用户名为必填项',
							trigger: 'blur'
						},
						{
							min: 5,
							max: 12,
							message: '长度在 5 到 12 个字符',
							trigger: 'blur'
						}
					],
					//校验密码
					password: [{
							required: true,
							message: '用户密码为必填项',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 10,
							message: '长度在 6 ~ 10 个字符',
							trigger: 'blur'
						}
					]
				}
			}
		},
		methods: {
			//点击注册按钮
			Register() {
				console.log('register');
				this.$router.replace( '/register');
			},
			//点击重置按钮，重置表单
			resetLoginForm() {
				this.$refs.loginFormRef.resetFields()
			},
			login() {
				this.$refs.loginFormRef.validate(async valid => {
					if (!valid) return;
					const {
						data: res
					} = await this.$http.post("login", this.loginform);

					if (res.flag == "ok") {
						this.$message.success("登录成功")

						var user = JSON.stringify(res.user);;

						window.sessionStorage.setItem("user", user) //存储user对象

						this.$router.push({
							path: "/home"
						});


					} else {
						this.$message.error("登录失败")
					}
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	//根节点样式
	.login-container {
		background-color: #4d3cac;
		height: 100%;
	}

	.login_box {
		border: 1px solid #eee;
		box-shadow: 0 0 10px #ddd;
		width: 450px;
		height: 300px;
		background-color: whitesmoke;
		border-radius: 20px; //盒子四角的曲率
		/* 设置位置 */
		position: absolute; //设置定位方式为ab方便精准设置位置
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%); //设置这个后位于正中央

		.avatar_box {
			width: 130px;
			height: 130px;
			border: 1px solid #eee;
			border-radius: 50%;
			padding: 5px;
			box-shadow: 0 0 2px #ddd;
			position: absolute;
			left: 50%;
			transform: translate(-50%, -50%);
			background-color: #eee;

			img {
				width: 100%;
				height: 100%;
				border-radius: 50%;
				background-color: #eee;
			}

		}
	}

	.btns {
		display: flex;
		justify-content: fix-end;
	}

	.login_from {
		position: absolute;
		bottom: 0%;
		width: 100%;
		padding: 0 10px;
		box-sizing: border-box;
	}
</style>
